package cn.soulic.tools.redis.migration.handler;

import cn.soulic.tools.redis.migration.config.RedisMigrationProperties;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import io.lettuce.core.ValueScanCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Set数据处理
 *
 * @author Soulic Yang
 * @since 1.0.0
 */
@Component(value = "set")
public class SetHandler extends AbstractHandler {
    private final Logger logger = LoggerFactory.getLogger(SetHandler.class);

    @Resource
    private RedisMigrationProperties properties;

    @Override
    public void doHandle(String key) {
        ScanCursor sScanCursor = ScanCursor.INITIAL;
        ScanArgs matchesAll = ScanArgs.Builder.matches(MATCHER_ALL).limit(properties.getMaxCount());

        do {
            ValueScanCursor<String> cursor = sourceCommands.sscan(key, sScanCursor, matchesAll);
            List<String> values = cursor.getValues();
            targetCommands.sadd(key, values.toArray(new String[0]));
            sScanCursor = cursor;
        } while (!sScanCursor.isFinished());
    }
}
